<?php
    include "../../../config.php";
    require_once("..`/redirect.php");
    require_once("../config.php");
    global $FOLIO_CFG;
    
    Header("content-type: application/x-javascript");
    
    $path = $CFG->wwwroot . 'mod/folio/ajax/';
    $url = $CFG->wwwroot;
    
    // Get the correct fieldname for the echo var in the Redirect object.
    //      This is then passed as the name of the parameter so that 
    //      the ajax pages know what to do as a resulting action.
    $redirect_echo = folio_redirect::htmlVariableName( 'echo' );

?>

// Upload file asynch thru ajax.
function init( iPage_Ident ){
    var onUploadButtonClick = function(e){
        //Set which form we're loading.
        YAHOO.util.Connect.setForm('folio_RichEditor_FileUpload_form', true);

        // Turn off form & show uploading icon
        //Effect.Fade('folio_RichEditor_FileUpload_form','appear');   
        Effect.Appear('folio_RichEditor_FileUpload_uploading','appear');

        var uploadHandler = {
            upload: function(o) {

                // Reset the upload box.
                //Effect.Appear('folio_RichEditor_FileUpload_form','appear');
                Effect.Fade('folio_RichEditor_FileUpload_uploading','appear');
                document.getElementById('folio_RichEditor_FileUpload_form').testFile.value = '';

                // Test to see if we have a valid result.
                if ( o.responseText.indexOf('|') == -1 || 
                     o.responseText.indexOf('~') == -1 ) {
                    // Tell the user of the error message.
                    alert( o.responseText );
                } else {
                    // Add the resource.
                    var sKey = o.responseText.substr( o.responseText.indexOf('|')+1, o.responseText.indexOf('~') - 1 - o.responseText.indexOf('|') );
                    var iPage = o.responseText.substr( o.responseText.indexOf('~') + 1 );
        
                    folio_RichEditor_AddResource( sKey, iPage );
                }
            }
        };
        YAHOO.util.Connect.asyncRequest('POST', '<?php echo $path; ?>file_insert.php', uploadHandler);
    };
    YAHOO.util.Event.on('uploadButton', 'click', onUploadButtonClick);
}


//Event.observe(window, 'load', init, false);
function folio_RichEditor_InitializeList( iPage ) {

    // Create list
    Sortable.create("folio_RichEditor_list",
        {
        dropOnEmpty:true,
        handle:"handle",
        containment:["folio_RichEditor_list"],
        constraint:false,
        onUpdate: function()
        {
            folio_RichEditor_SetOrder( iPage );
        }

        });

}

// Called by the returns from ajax postbacks to re-set the sort order, as well as
//      by the onUpdate property of the Sortable.
function folio_RichEditor_SetOrder( iPage ) {

    var poststring = Sortable.sequence('folio_RichEditor_list');

    // This doesn't actually update part of the page.  It is needed to simply
    //      set the new sort order after adding or updating the page.
    var myR = new Ajax.Updater(   'no_update_location_123_123_123', //folio_RichEditor_list
        '<?php echo $path; ?>page_update.php',
        {   insertion: Insertion.Top, 
            asynchronous:false,
            evalScripts:true,
            method: 'post',
            parameters: 'page_ident='+iPage + '&<?php echo $redirect_echo; ?>=body' +'&content='+poststring});

}
// Called to load menu of items from the db for the left-hand side.
function folio_RichEditor_GetResources( sType, iUser, iPage ) {
    var myA = new
        Ajax.Updater('folio_resources', 
            '<?php echo $path; ?>resource_select.php', 
            {method: 'get', parameters: 'type='+sType+'&page_owner='+iUser+'&page_ident='+iPage});
}

// Called to setup a new resource item, moving from a resource to an item.
// ID : the id of the resource to change into an item.    
function folio_RichEditor_AddResource( sKey, iPage  ) {

    Sortable.destroy("folio_RichEditor_list");

    // Load from db.  Note that this is asynch, executes returned js code.
    var myR = new Ajax.Updater(   'folio_RichEditor_list', // + id, 
        '<?php echo $path; ?>artifact_insert.php', 
        {   insertion: Insertion.Top, 
            asynchronous:false,
            evalScripts:true,
            method: 'post', 
            parameters: 'key='+sKey+'&page_ident='+iPage});

    //Effect.Fade('folio_RichEditor_working','appear');
}

// Called to setup a new resource item, moving from a resource to an item.
// ID : the id of the resource to change into an item.    
function folio_Decoration_AddText( iPage  ) {

    Sortable.destroy("folio_RichEditor_list");

    // Load from db.  Note that this is asynch, executes returned js code.
    var myR = new Ajax.Updater(   'folio_RichEditor_list', // + id, 
        '<?php echo $path; ?>decoration_insert.php', 
        {   insertion: Insertion.Top, 
            asynchronous:false,
            evalScripts:true,
            method: 'post', 
            parameters: 'page_ident='+iPage+'&type=label'});
            
    //Effect.Fade('folio_RichEditor_working','appear');
}


// Called to remove a resource item
// ID : the id of the resource 
function folio_RichEditor_Delete( iIdent, sType ) {

    // Remove JS element.
    Element.remove('folio_'+sType + iIdent);    

    // Remove from db via ajax call.
    var myR = new Ajax.Updater(   'folio_RichEditor_list',
        '<?php echo $path; ?>'+sType+'_delete.php', 
        {   insertion: Insertion.Top, 
            asynchronous:false,
            evalScripts:true,
            method: 'post', 
            parameters: sType+'_ident='+iIdent});

}



// Toggle visibility of object.
function folio_artifact_ToggleVisible( sElement_id, boolVisible ) {
    if ( boolVisible ) {
        Effect.Appear(sElement_id +'_text','appear');
        Effect.Fade(sElement_id+ '_expand','appear');   
    } else {
        Effect.Fade(sElement_id +'_text','appear');   
        Effect.Appear(sElement_id +'_expand','appear');
    }
}


<?php
    unset( $path );
    unset( $redirect_echo );
?>